﻿//////////////////////////////////////////////////////////////////////////////
//                                                                          //
// Copyright 2010 ZGDev Software                                            //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//   http://www.apache.org/licenses/LICENSE-2.0                             //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////

using System;

namespace ZGDev.FormSpark
{
    /// <summary>
    /// Called by the main thread dispatcher on the main application thread.
    /// </summary>
    /// <param name="entity">The entity that requested the callback.</param>
    /// <param name="arg">An (optional) argument that is to be passed to the entity.</param>
    internal delegate void MainThreadCallbackDelegate(IMainThreadEntity entity, object arg);

    #region IMainThreadEntity

    /// <summary>
    /// Describes an entity that can be called back on the main thread.
    /// </summary>
    /// <seealso cref="IMainThreadDispatcher"/>
    internal interface IMainThreadEntity
    {
        /// <summary>
        /// Called on the main thread by the main thread dispatcher.
        /// </summary>
        /// <param name="arg">An (optional) argument for user data.</param>
        void MainThreadCallback(object arg);
    }

    #endregion

    #region IMainThreadDispatcher

    /// <summary>
    /// Describes a "main thread dispatcher." Main thread dispatchers allow |IMainThreadEntity|s
    /// to request that they have their |MainThreadCallback| method be called back on the main
    /// thread.
    /// </summary>
    /// <seealso cref="Dispatch"/>
    /// <seealso cref="IMainThreadEntity"/>
    /// <seealso cref="MainThreadCallerControl"/>
    /// <seealso cref="MainThreadCallerWPFControl"/>
    internal interface IMainThreadDispatcher
    {
        /// <summary>
        /// Determines if a dispatch is needed (a dispatch is not needed if the caller is executing
        /// on the main thread already).
        /// </summary>
        /// <returns>True if the caller is not executing on the main thread, false otherwise.</returns>
        bool GetNeedsDispatch();

        /// <summary>
        /// Requests that an entity be called called back on the main thread.
        /// </summary>
        /// <param name="entity">The entity that is to be called back.</param>
        /// <param name="arg">The (optional) argument to pass to the entity.</param>
        void RequestMainThreadCallback(IMainThreadEntity entity, object arg);
    }

    #endregion
}
